//
//  HJGroupInviteController.m
//  KaiChat
//
//  Created by lx on 2023/11/14.
//

#import "HJGroupInviteController.h"
#import "HJGroupInviteCell.h"
#import "HJIM.h"
#import "TJEmptyDataHandler.h"

@interface HJGroupInviteController ()

@property (nonatomic, strong) NSArray *datas;
@property (nonatomic, strong) TJEmptyDataHandler *emptyDataHandler;

@end

@implementation HJGroupInviteController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.navigationBar.title = @"Join Application";
    
    self.view.backgroundColor = UIColor.whiteColor;
    self.tableViewStyle = UITableViewStylePlain;
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, TabbarSafeBottomMargin, 0);
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 0.1)];
    [self.tableView registerClass:HJGroupInviteCell.class forCellReuseIdentifier:@"HJGroupInviteCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.rowHeight = 60;
    self.tableView.backgroundColor = UIColor.whiteColor;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(KNavBarHight);
        make.left.right.bottom.offset(0);
    }];
    
    _emptyDataHandler = [TJEmptyDataHandler emptyDataHandleWithScrollView:self.tableView];

    [HJHUD showLoadingInView:self.view];
    [self loadData];
}

- (void)loadData {
    [HJIM.shareIM.groupManager loadGroupInviteListWithGroupId:self.groupId completion:^(NSArray<HJGroupInviteModel *> * _Nullable datas) {
        self.datas = datas;
        self.emptyDataHandler.canDisplay = YES;
        [self.tableView reloadData];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HJGroupInviteCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HJGroupInviteCell" forIndexPath:indexPath];
    HJGroupInviteModel *model = self.datas[indexPath.row];
    cell.model = model;
    TJWeakify(self)
    [cell setDidClickAgreeButtonBlock:^{
        [HJHUD showGraceTimeLoadingInView:weakself.view];
        [HJIM.shareIM.groupManager agreeUserJoinGroupByGroupId:self.groupId userIds:@[model.userId] completion:^(NSError * _Nullable error) {
            if (error) {
                [HJHUD showErrorMessageTip];
                return;
            }
            [HJHUD showSucceedMessageTip];
            [self loadData];
        }];
    }];
    return cell;
}
 

@end
